package com.zcxy.fast.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxy.fast.bean.PageQuery;
import com.zcxy.fast.entity.SysTest;
import com.zcxy.fast.mapper.SysTestMapper;
import com.zcxy.fast.mybatisExtend.BaseServiceImpl;
import com.zcxy.fast.mybatisExtend.RelationHelper;
import com.zcxy.fast.service.ISysAttachmentService;
import com.zcxy.fast.service.ISysTestService;
import com.zcxy.fast.utils.BeanCopyUtil;
import com.zcxy.fast.utils.StringUtils;
import com.zcxy.fast.vo.SysCategoryVo;
import com.zcxy.fast.vo.SysTestVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 单表代码生成组件以及校验测试业务层处理
 *
 * @author zcc
 * @email 860474958@qq.com
 * @date 2021-01-07 11:18:42
 */
@Service
@Slf4j
public class SysTestServiceImpl extends BaseServiceImpl<SysTestMapper, SysTest> implements ISysTestService {

    @Autowired
    private ISysAttachmentService iSysAttachmentService;


    @Override
    public IPage<SysTestVo> queryPageList(PageQuery<SysTest> queryVo) {
        SysTest sysTest = queryVo.getT() == null ? new SysTest() : queryVo.getT();
        QueryWrapper queryWrapper = new QueryWrapper();
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());
        if (StringUtils.isNotEmpty(sysTest.getTinput())) {
            queryWrapper.like("tinput", sysTest.getTinput());
        }
        if (StringUtils.isNotEmpty(sysTest.getTextarea())) {
            queryWrapper.eq("textarea", sysTest.getTextarea());
        }
        IPage<SysTest> sysUserVoIPage = baseMapper.selectPage(page, queryWrapper);
        IPage<SysTestVo> sysTestVoIPage = BeanCopyUtil.copyPage(sysUserVoIPage, SysTestVo.class);
        if (CollectionUtil.isNotEmpty(sysTestVoIPage.getRecords())) {
            RelationHelper.renderList(sysTestVoIPage.getRecords());
        }
        return sysTestVoIPage;
    }

    @Override
    public List<SysTestVo> queryList(SysTest sysTest) {
        SysTest sysTestq = sysTest == null ? new SysTest() : sysTest;
        QueryWrapper queryWrapper = new QueryWrapper(sysTestq);
        List<SysTestVo> SysTestVos = BeanCopyUtil.copyListToList(baseMapper.selectList(queryWrapper), SysTestVo.class);
        return SysTestVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysTest(SysTest sysTest) {
        baseMapper.insert(sysTest);
        iSysAttachmentService.updateOrAddSysAttachments(sysTest.getTextarea(), sysTest.getTextareaAttachs());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysTest(SysTest sysTest) {
        baseMapper.updateById(sysTest);
        iSysAttachmentService.updateOrAddSysAttachments(sysTest.getTextarea(),sysTest.getTextareaAttachs());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAllSysTest(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);
    }


}
